package com.kotiln.lgc.lib_voice.engine

import com.baidu.speech.utils.LogUtil
import com.imooc.lib_voice.words.NluWords
import com.kotiln.lgc.lib_voice.impl.OnNluResultListener
import org.json.JSONArray
import org.json.JSONObject

/**
 *@fileName VoiceEngineAnalyze
 *@date2021/1/25  9:54
 *@author VoiceEngineAnalyze$
 *@describe TODO
 *@desc 语音引擎分析
 */
object VoiceEngineAnalyze {

    private var TAG: String = VoiceEngineAnalyze::class.java.simpleName

    private lateinit var onNluResultListener: OnNluResultListener

    //分析结果
    fun analyzeNlu(nlu: JSONObject, onNluResultListener: OnNluResultListener) {
        this.onNluResultListener = onNluResultListener
        //用户说的话
        val rawText: String = nlu.optString("raw_text")
//        {"appid":15373,"encoding":"UTF-8","results":[{"domain":"weather",
//        "intent":"USER_WEATHER","score":100,"slots":
//        {"user_loc":[{"word":"北京","norm":"(NERL_PLUS_LOC_ROOT)>北京市"}]}}],
//        "err_no":0,"parsed_text":"北京 的 天气 怎么 样 ？","raw_text":"北京的天气怎么样？"}
        LogUtil.i("rawText:$rawText")
        //解析results
        val results: JSONArray? = nlu.optJSONArray("results")
        val nluResultsLength: Int? = results?.length()
        if (nluResultsLength != null) {
            when {
                nluResultsLength <= 0 -> return
                //单条命中
                nluResultsLength == 1 -> analyzeNluSingle(results[0] as JSONObject)
                else -> {
                    //多条命中
                }
            }
        }
    }

    //处理单条结果
    private fun analyzeNluSingle(result: JSONObject) {
        val domain: String = result.optString("domain")
        val intent: String = result.optString("intent")
        val slots: JSONObject = result.optJSONObject("slots")

        slots?.let {
            when (domain) {
                NluWords.NLU_APP -> {
                    when (intent) {
                        NluWords.INTENT_OPEN_APP,
                        NluWords.INTENT_UNINSTALL_APP,
                        NluWords.INTENT_UPDATE_APP,
                        NluWords.INTENT_DOWNLOAD_APP,
                        NluWords.INTENT_SEARCH_APP,
                        NluWords.INTENT_RECOMMEND_APP-> {
                            //获取到 App的名称
                            val userAppName = it.optJSONArray("user_app_name")
                            userAppName?.let { appName ->
                                if (appName.length() > 0) {
                                    val obj: JSONObject = appName[0] as JSONObject
                                    val word:String=obj.optString("word")
                                    when (intent) {
                                        NluWords.INTENT_OPEN_APP ->
                                            onNluResultListener.openApp(word)

                                        NluWords.INTENT_UNINSTALL_APP ->
                                            onNluResultListener.unInstanllApp(word)

                                        else ->
                                            //其它App操作
                                            onNluResultListener.otherApp(word)

                                    }

                                } else {
                                    onNluResultListener.nluError()
                                }
                            }
                        }
                        else -> {
                            //其他 App操作
                            onNluResultListener.nluError()
                        }
                    }
                }
                else -> {
                    onNluResultListener.nluError()
                }
            }
        }
    }
}